package com.xing.dao.impl;

import com.xing.dao.AccountDao;
import com.xing.domain.Account;
import com.xing.util.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;

/*
 * 静，谦虚谨慎，心胸宽广容纳所有好的坏的、先苦后甜
 * day41_transaction
 * 2021/5/8 20:41
 */
public class AccountDaoImpl implements AccountDao {

    private QueryRunner qr=new QueryRunner();
    @Override
    public Account select(String cardNo, String pwd) {
        Connection connection = DataSourceUtils.getConnection();
        try {
            return qr.query(connection,"select * from account where cardNo=? and password=?",new BeanHandler<>(Account.class),cardNo,pwd);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            DataSourceUtils.closeAll(null, null, connection);
        }

    }


    @Override
    public boolean select(String CardNo) {
        Connection connection = DataSourceUtils.getConnection();
        try {
            Account account=qr.query(connection,"select * from account where cardNo=?", new BeanHandler<>(Account.class),CardNo);
            return account!=null?true:false;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            DataSourceUtils.closeAll(null, null, connection);
        }
    }

    @Override
    public BigDecimal selectMoney(String cardNo) {
        Connection connection = DataSourceUtils.getConnection();
        try {
            Account account=qr.query(connection,"select * from account where cardNo=?", new BeanHandler<>(Account.class),cardNo);
            if(account!=null){
                return account.getMoney();
            }
            return new BigDecimal(0);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            DataSourceUtils.closeAll(null, null, connection);
        }
    }

    @Override
    public void updateTake(String cardNo, BigDecimal money) {
        Connection connection = DataSourceUtils.getConnection();
        try {
            qr.update(connection,"update account set money=money-? where cardNo=? ", money,cardNo);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            DataSourceUtils.closeAll(null, null, connection);
        }
    }

    @Override
    public void updateSave(String cardNo, BigDecimal money) {
        Connection connection = DataSourceUtils.getConnection();
        try {
            qr.update(connection,"update account set money=money+? where cardNo=? ", money,cardNo);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            DataSourceUtils.closeAll(null, null, connection);
        }
    }
}
